package com.example.article.controller;

import com.example.article.entity.Comment;
import com.example.article.service.CommentService;
import com.example.commonutils.JwtUtils;
import com.example.commonutils.ResultData;
import com.example.servicebase.exceptionhandler.CustomException;
import io.netty.util.internal.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.http.MediaType;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/article/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;

    //发表评论
    @PostMapping("saveComment/{articleId}")
    public ResultData saveComment(
            @PathVariable("articleId") String articleId,
            @RequestBody(required = false) Comment comment,
            HttpServletRequest request){
//        context=context.substring(1,context.length()-1);
        //从请求中获取token
        String token = JwtUtils.getMemberIdByJwtToken(request);
        if (StringUtil.isNullOrEmpty(token))
            throw new CustomException(20001,"请先登录");
        return commentService.saveCommentById(articleId,comment,request);
    }

    @GetMapping(value = "commentCount/{articleId}/{limit}",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultData commentCount(@PathVariable("articleId") String articleId ,
                                   @PathVariable("limit") Integer limit,
                                   HttpServletRequest request){
        return commentService.getComments(articleId,limit,request);
    }
    //删除评论
    @DeleteMapping("delComment/{commentId}")
    public ResultData delComment(@PathVariable String commentId,HttpServletRequest request){
        return commentService.delComment(commentId,request);
    }
}
